package com.xoa.service.party;


import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.party.InspectorProblemMapper;
import com.xoa.model.party.InspectorProblem;
import com.xoa.model.users.Users;
import com.xoa.util.ExcelUtil;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.page.PageParams;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 程叶同 on 2018/1/6.
 * 空气重污染 督查问题汇总
 */
@Service
public class InspectorProblemService {
    @Autowired
    InspectorProblemMapper inspectorProblemMapper;
    @Autowired
    DepartmentMapper departmentMapper;
    /**
    * @Author 程叶同
    * @Description 督查问题添加
    * @Date 10:34 2018/1/6
    */
    public BaseWrapper addProblem(InspectorProblem inspectorProblem){
        BaseWrapper baseWrapper=new BaseWrapper();
        if(inspectorProblem!=null){
            int insert = inspectorProblemMapper.insert(inspectorProblem);
            if(insert>0){
                baseWrapper.setFlag(true);
                baseWrapper.setMsg("添加成功");
            }else{
                baseWrapper.setMsg("添加失败 ");
                baseWrapper.setFlag(false);
            }
        }else{
            baseWrapper.setFlag(false);
            baseWrapper.setMsg("参数错误");
        }
        return  baseWrapper;
    }
    /**
    * @Author 程叶同
    * @Description 督查问题 删除
    * @Date 10:43 2018/1/6
    */
    public BaseWrapper deleteProblem(HttpServletRequest request, Integer id){
        BaseWrapper baseWrapper=new BaseWrapper();
        Users users= SessionUtils.getSessionInfo(request.getSession(), Users.class,new Users());
        InspectorProblem inspectorProblem = inspectorProblemMapper.selectByPrimaryKey(id);
        if(inspectorProblem!=null){

                int i = inspectorProblemMapper.deleteByPrimaryKey(id);
                if(i>0){
                    baseWrapper.setMsg("删除成功");
                    baseWrapper.setFlag(true);
                }else{
                    baseWrapper.setFlag(false);
                    baseWrapper.setMsg("删除失败");
                }
        }
        return  baseWrapper;
    }

    /**
    * @Author 程叶同
    * @Description 督查问题修改
    * @Date 11:03 2018/1/6
    */
    public BaseWrapper updateProblem(HttpServletRequest request, InspectorProblem inspectorProblem){
        BaseWrapper baseWrapper=new BaseWrapper();
        Users users= SessionUtils.getSessionInfo(request.getSession(), Users.class,new Users());
        if(inspectorProblem!=null){

                inspectorProblemMapper.updateByPrimaryKey(inspectorProblem);
                baseWrapper.setFlag(true);
                baseWrapper.setMsg("修改成功");

        }else{
            baseWrapper.setMsg("参数错误");
            baseWrapper.setFlag(false);
        }
        return baseWrapper;
    }

    /**
    * @Author 程叶同
    * @Description 督查问题 查询
    * @Date 11:12 2018/1/6
    */
    public BaseWrapper getProblemList(HttpServletRequest request, Integer page, Integer pageSize, String subcounty, String unitName){
        Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class,new Users());
        BaseWrapper baseWrapper=new BaseWrapper();
        List<InspectorProblem> resultlist=new ArrayList<InspectorProblem>();
        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,Object> map1=new HashMap<String,Object>();
        map.put("unitName",unitName);
        map1.put("unitName",unitName);
        PageParams pageParams=new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        map.put("page",page-1);
        map.put("pageParams",pageParams);
        map.put("subcounty",subcounty);
        map1.put("unitName",unitName);
        int total=0;
        //获取所属部门
        resultlist=inspectorProblemMapper.selectList(map);
        total=inspectorProblemMapper.totalItems(map1);
        /*Department department=departmentMapper.getDeptById(user.getDeptId());
        if(department.getDeptParent()==27){
            //属于社区



        }else{
            resultlist=inspectorProblemMapper.selectList(map);
            total=inspectorProblemMapper.totalItems(map1);
        }*/
        if(resultlist!=null){
            /*List<InspectorProblemExtend> resultlists=new ArrayList<InspectorProblemExtend>();
            for(InspectorProblem s:resultlist){
                InspectorProblemExtend ie=new InspectorProblemExtend();
                BeanUtils.copyProperties(s,ie);
                ie.setCommitTimeStr(s.getCommitTime().toString());
                resultlists.add(ie);
            }*/
            baseWrapper.setFlag(true);
            baseWrapper.setData(resultlist);
            baseWrapper.setMsg("查询成功");
        }else{
            baseWrapper.setMsg("error");
            baseWrapper.setFlag(false);
        }
        return  baseWrapper;
    }

    /**
    * @Author 程叶同
    * @Description 查询单条记录
    * @Date 11:27 2018/1/6
    */
    public BaseWrapper getInspectorById(Integer id){
        BaseWrapper baseWrapper=new BaseWrapper();
        if(id!=null){
            InspectorProblem inspectorProblem = inspectorProblemMapper.selectByPrimaryKey(id);
            if(inspectorProblem!=null){
                baseWrapper.setFlag(true);
                baseWrapper.setData(inspectorProblem);
                baseWrapper.setMsg("查询成功");
            }else{
                baseWrapper.setMsg("查询失败");
                baseWrapper.setFlag(false);
            }
        }else{
            baseWrapper.setFlag(false);
            baseWrapper.setMsg("参数错误");
        }
        return baseWrapper;
    }

    public BaseWrapper outPutFile(HttpServletRequest request, HttpServletResponse response, String ids){
        BaseWrapper baseWrapper=new BaseWrapper();
        List<InspectorProblem> inspectorProblems=null;
        if(ids!=null&&ids!=""){
            String[] id=ids.split(",");
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("id",id);
            inspectorProblems=inspectorProblemMapper.selectList(map);
        }
        try {

            HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("督查问题汇总表", 9);
            String[] secondTitles = {"单位名称/点位","区","存在问题","处理方式","问题整改情况","提交人"};
            HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);

            // String[] beanProperty = {user.getDep().getDeptName(),"userName","userPrivName", "roleAuxiliaryName","online","sex","online","telNoDept","telNoDept","departmentPhone","email"};
            String[] beanProperty = {"unitName","area","existingProblem","processType","problemSuituation","userName"};

            HSSFWorkbook workbook = ExcelUtil.exportExcelData(workbook2, inspectorProblems, beanProperty);
            ServletOutputStream out = response.getOutputStream();

            String filename = "督查问题汇总表.xls";
            filename = FileUtils.encodeDownloadFilename(filename,
                    request.getHeader("user-agent"));
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-disposition",
                    "attachment;filename=" + filename);
            workbook.write(out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        return  baseWrapper;
    }

}
